//防抖
const debounce = function (fn, args, wait = 0) {
    let timeout = null
    return function () {
        if (timeout !== null) { clearTimeout(timeout) }// 如果多次触发将上次记录延迟清除掉
        timeout = setTimeout(() => {
            fn(args)
            timeout = null
        }, wait)
    }
}


document.addEventListener('DOMContentLoaded', () => {
    const slides = document.querySelectorAll('.slide')
    const slideWrap = document.querySelector('.slide-wrap')
    let currentSlideIndex = 0

    const setCurrentSlide = (event) => {
        //防止页面滚动
        event.preventDefault();
        console.log(event.deltaY);
        if (event.deltaY > 0) {
            // 向下滚动，执行下一页操作
            if (currentSlideIndex < slides.length - 1) {
                // 更新当前页码
                currentSlideIndex++;
                slideWrap.style.transform = `translateY(-${currentSlideIndex * 100}vh)`
            }
        } else {
            // 向上滚动，执行上一页操作
            if (currentSlideIndex > 0) {
                // 更新当前页码
                currentSlideIndex--;
                slideWrap.style.transform = `translateY(-${currentSlideIndex * 100}vh)`
            }
        }

    }

    slideWrap.addEventListener('wheel', (event) => {
        debounce(setCurrentSlide, event, 50)()
    }, false)
})

